﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LSEntity;
using LSUtility;
using MySql.Data.MySqlClient;

namespace LSData
{
    public class TestData : DataBase
    {
        public static List<TestEntity> TestGetList(TestGetEntity testGetEntity)
        {
            const string testAs = "t";
            const string sseattestSchoolAs = "ts";
            const string schoolParame = "School";
            const string curriculumParame = "Curriculum";
            const string testIdParame = "TestId";
            var where = String.Empty;
            var param = new List<MySqlParameter>();
            if (testGetEntity.SchoolId > 0)
            {
                where += String.Format(" and {0}.schoolid=@{1}", sseattestSchoolAs, schoolParame);
                param.Add(MakeParameter(schoolParame, MySqlDbType.Int32, testGetEntity.SchoolId));
            }
            if (testGetEntity.CurriculumId.ListGetCount() > 0)
            {

                var ids = new List<int>();
                testGetEntity.CurriculumId.ForEach(m => ids.Add((int)m));
                param.Add(MakeParameter(curriculumParame, MySqlDbType.String, ids.List2Str(m => m)));
                where += String.Format(" and {1}.Curriculum_ID in(@{0})", curriculumParame, testAs);
            }
            if (testGetEntity.TestId > 0)
            {
                where += String.Format(" and {0}.Test_ID=@{1}", testAs, testIdParame);
                param.Add(MakeParameter(testIdParame, MySqlDbType.Int32, testGetEntity.TestId));
            }
            var sql = String.Format(@"select 
                                        {1}.Test_ID as Id,
                                        {1}.Creator_ID as Creator,
                                        {1}.Curriculum_ID as CurriculumId,
                                        {1}.Create_Time as CreateTime,
                                        {1}.Name as Name,
                                        {1}.Test_Type as TestType,
                                        {1}.Grade_ID as GredeId,
                                        {1}.Module_ID as ModuleId,
                                        {1}.Unit_ID as UnitId,
                                        {1}.Assignment_ID as AssignmentId,
                                        {1}.Password as Password,
                                        {1}.isRelease as IsRelease,
                                        {1}.Test_Class as TestClass
                                        from sseat_test_school as {2} 
                                        join test as {1} on ts.testid={1}.Test_ID 
                                        where 1=1{0} order by {1}.Test_ID desc",
                                    where,
                                    testAs,
                                    sseattestSchoolAs);
            return GetList<TestEntity>(sql, EConn, param).ToList();
        }

        public static bool TestSetClass(TestClassEntity testClassEntity)
        {
            var model =
                GetInfo<TestClassEntity>(
                    String.Format("select * from test_class_mapping where TestId={0} and ClassId={1}",
                                  testClassEntity.TestId,
                                  testClassEntity.ClassId), EConn);
            return model == null
                       ? Insert(testClassEntity, "insert", EConn, false) > 0
                       : Update(testClassEntity, String.Format(" where TestId={0} and ClassId={1}",
                                                               testClassEntity.TestId,
                                                               testClassEntity.ClassId), "update", EConn);
        }

        public static TestClassEntity TestGetClassGet(int testId, int accountId)
        {
            var sql = String.Format(@"select tcm.*,c.Name as ClassName,t.Name as TestName 
                                        from test_class_mapping as tcm 
                                        join class as c on c.ID=tcm.ClassId 
                                        join test as t on t.Test_ID=tcm.TestId 
                                        where tcm.TestId={0} and tcm.CreatorId={1}",
                                    testId, accountId);
            return GetInfo<TestClassEntity>(sql, EConn);
        }

        public static List<TestClassEntity> TestGetClassGetList(List<int> classIds)
        {
            var sql = String.Format(@"select tcm.*,c.Name as ClassName,t.Name as TestName 
                                        from test_class_mapping as tcm 
                                        join class as c on c.ID=tcm.ClassId 
                                        join test as t on t.Test_ID=tcm.TestId 
                                        where ClassId in({0})", classIds.List2Str(m => m));
            return GetList<TestClassEntity>(sql, EConn).ToList();
        }
    }
}
